﻿using MobaCommon.Code;
using MobaCommon.OpCode;
using MOBAServer.Room;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace MOBAServer.Cache
{
    public class SelectCache:RoomCacheBase<SelectRoom>
    {
        /// <summary>
        /// 玩家下线
        /// </summary>
        /// <param name="client"></param>
        public void Offline(MobaClient client,int playerId)
        {
            int roomId;
            if (!playerRoomDic.TryGetValue(playerId, out roomId))
                return ;
            SelectRoom room;
            if (!idRoomDic.TryGetValue(roomId, out room))
                return ;
            //移除退出的客户端
            room.clientList.Remove(client);
            //给剩余的客户端发消息：房间有人退出  解散  回到主界面
            room.Broast(OpCode.SelectCode,OpSelect.Destory,0,"有人退出",null);
            Destroy(roomId);
        }
        /// <summary>
        /// 开始选人
        /// </summary>
        public void CreateRoom(List<int> team1,List<int> team2)
        {
            SelectRoom room = null;
            if(!roomQue.TryDequeue(out room))
            {
                room = new SelectRoom(id,team1.Count+team2.Count);
                id++;
            }
            room.InitRoom(team1,team2);
            //绑定玩家ID和房间ID
            foreach (var item in team1)
            {
                playerRoomDic.TryAdd(item,room.id);
            }
            foreach (var item in team2)
            {
                playerRoomDic.TryAdd(item, room.id);
            }
            //绑定房间ID和房间
            idRoomDic.TryAdd(room.id,room);
            //开启一个定时任务，通知玩家在10秒之内进入房间，否则房间自动销毁
            room.StartSchedule(DateTime.UtcNow.AddSeconds(10),()=> {
                //销毁房间
                if(!room.IsAllEnter)
                {
                    Destroy(room.id);
                    room.Broast(OpCode.SelectCode,OpSelect.Destory,0,"有人未进入 重新回到匹配队列");
                }
            });
        }
        /// <summary>
        /// 销毁房间
        /// </summary>
        /// <param name="roomId"></param>
        public void Destroy(int roomId)
        {
            SelectRoom room = null;
            if(!idRoomDic.TryRemove(roomId,out room))
                return;
            //移除玩家ID和房间ID的关系
            foreach (var item in room.team1Dic.Keys)
            {
                playerRoomDic.TryRemove(item,out roomId);
            }
            foreach (var item in room.team2Dic.Keys)
            {
                playerRoomDic.TryRemove(item, out roomId);
            }
            //清空房间内的数据
            room.team1Dic.Clear();
            room.team2Dic.Clear();
            room.enterCount = 0;
            room.readyCount = 0;
            room.clientList.Clear();
            //入队列
            roomQue.Enqueue(room);
        }
        /// <summary>
        /// 选择英雄
        /// </summary>
        /// <returns></returns>
        public SelectRoom Select(int playerId,int heroId)
        {
            int roomId;
            if (!playerRoomDic.TryGetValue(playerId, out roomId))
                return null;
            SelectRoom room;
            if (!idRoomDic.TryGetValue(roomId, out room))
                return null;
            if (room.Select(playerId, heroId))
                return room;
            else
                return null;
        }
        /// <summary>
        /// 确认选择
        /// </summary>
        /// <param name="playerId"></param>
        /// <returns></returns>
        public SelectRoom Ready(int playerId)
        {
            int roomId;
            if (!playerRoomDic.TryGetValue(playerId, out roomId))
                return null;
            SelectRoom room;
            if (!idRoomDic.TryGetValue(roomId, out room))
                return null;
            return room.Ready(playerId) ? room : null;
        }
        /// <summary>
        /// 进入房间
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public SelectRoom Enter(int playerId,MobaClient client)
        {
            int roomId;
            if (!playerRoomDic.TryGetValue(playerId, out roomId))
                return null;
            SelectRoom room;
            if (!idRoomDic.TryGetValue(roomId, out room))
                return null;
            room.Enter(playerId,client);
            return room;
        }

        public SelectRoom GetRoom(int playerId)
        {
            int roomId;
            if (!playerRoomDic.TryGetValue(playerId, out roomId))
                return null;
            SelectRoom room;
            if (!idRoomDic.TryGetValue(roomId, out room))
                return null;
            return room;
        }
    }
}
